#include "ctool.h"
#include "tool.h"
ctool::ctool(QObject *parent):QObject(parent){}

void ctool::processingCommunicationInformationCenter(QByteArray communicationInformationList)
{
    QByteArrayList clientToServiceCoomunicationByteArrayList
        =byteArrayTolist(communicationInformationList,this->dedicatedDelimiter);
    for(size_t i=0;i<clientToServiceCoomunicationByteArrayList.size();i++){
        processingCommunicationInformation(clientToServiceCoomunicationByteArrayList[i]);
    }
}




void ctool::processingCommunicationInformation(QByteArray communicationInformation)
{
    QByteArray byteArraycCopy=communicationInformation;
    QJsonDocument jsonDoc=QJsonDocument::fromJson(byteArraycCopy);
    if(jsonDoc.isObject()){
        QJsonObject jsonOBJ=jsonDoc.object();
        qDebug()<<jsonOBJ;
        QStringList jsonKEYS=jsonOBJ.keys();
        if(!jsonKEYS.contains("action"))
            return;//无action 信息错误
        switch (identifierOfProcessedInformation[jsonOBJ["action"].toString()]) {
        case 0:{//LoginBack 登录回谈
            // qDebug()<<jsonOBJ["code"];
            bool isLoginSuc=(jsonOBJ["code"].toString()=="1");

            QString isVip=jsonOBJ["isVip"].toString();
            QString VipKind=jsonOBJ["VIP"].toString();
            QString id=jsonOBJ["ID"].toString();
            QString name=jsonOBJ["NAME"].toString();

            Tool::getStaticTool()->setId(id);
            Tool::getStaticTool()->setIsVip(isVip);
            Tool::getStaticTool()->setVip(VipKind);
            Tool::getStaticTool()->setName(name);

            if(Tool::getStaticTool()->getIsToSaveToLocation()){
                Tool::getStaticTool()->createAutoLoginFile(Tool::getStaticTool()->getTmpAcc(),Tool::getStaticTool()->getTmpPsw());
            }

            Tool::getStaticTool()->setIsLogined(true);
            emit Tool::getStaticTool()->loginSuc();
            // qDebug()<<isVip<<VipKind<<id<<name;
            break;
        }


        default:{
            break;

        }

        }
    }
}

QByteArrayList ctool::byteArrayTolist(QByteArray byteArray, QByteArray dedicateDelimter)
{
    long long from=0;
    int size=dedicateDelimter.size();
    QByteArrayList targetByteArrayList;
    while(from<byteArray.size()-1){
        long long endOfSegmentedCharacter=byteArray.indexOf(dedicateDelimter,from);
        QByteArray partByteArray=byteArray.mid(from,endOfSegmentedCharacter);
        targetByteArrayList.append(partByteArray);
        from=endOfSegmentedCharacter+size;
    }
    return targetByteArrayList;
}
